<?php

namespace app\api\lists;


use app\common\model\finance\TransactionRecord;
use app\common\lists\ListsSearchInterface;
use app\common\model\user\User;




/**
 * transaction列表
 * Class TransactionRecordLists
 * @package app\api\lists
 */
class TransactionDailyReportLists extends BaseApiDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2025/04/09 14:11
     */
    public function setSearch(): array
    {
        $allowSearch = ['user_id', 'create_time_start', 'create_time_end','group_by'];
        return array_intersect(array_keys($this->params), $allowSearch);
    }

    public function queryWhere()
    {
        // 指定客户
        $where[] = ['group_id', '=', $this->userInfo['group_id']];


        // 指定用户
        if (isset($this->params['user_id'])) {
            $where[] = ['user_id', 'in', $this->params['user_id']];
        }

        return $where;
    }

    /**
     * @notes 获取transaction列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2025/04/09 14:11
     */
    public function lists(): array
    {
        $lists = TransactionRecord::field([
            'user_id',
            'DATE(transaction_date) as transaction_date',
            'COUNT(*) as transaction_count',
            'SUM(ABS(service_amount)) as service_amount',
            'SUM(ABS(transaction_amount)) as transaction_amount',
            'SUM(ABS(billing_amount)) as billing_amount',       
            // 新增
            'SUM(CASE WHEN transaction_type = "declined" AND status = "failed" THEN 1 ELSE 0 END) as failed_transaction_count',
            'SUM(CASE WHEN transaction_type = "declined" AND status = "failed" THEN ABS(billing_amount) ELSE 0 END) as declined_failed_amount'
        ])
            ->where($this->queryWhere())
            ->where('transaction_type', 'in', ['CARD_PAYMENT', 'CLEARING', 'settled', 'pending', 'AUTHORIZATION', 'declined'])
            ->where('status', 'in', ['SUCCESS', 'CANCELLED', 'settled', 'posted', 'pending', 'failed'])
            ->withSearch($this->setSearch(), $this->params)
            ->order('transaction_date DESC, user_id')
            ->select()
            ->toArray();
        foreach ($lists as &$item) {
            $user = User::where('id', $item['user_id'])->find();
            $item['nickname'] = $user['nickname']??'';
            $item['role_id'] = $user['role_id']??0;
            $item['nickname2'] = '';
            if($user['pid']!=$user['id']){
                $user = User::where('id', $user['pid'])->find();
                $item['nickname2'] = $user['nickname']??'';
            }
            $item['billing_amount'] = abs(floatval($item['billing_amount'])) + abs(floatval($item['service_amount']));
        }
        unset($item);
        return $lists;
    }


    /**
     * @notes 获取transaction数量
     * @return int
     * @author likeadmin
     * @date 2025/04/09 14:11
     */
    public function count(): int
    {
        return TransactionRecord::group('DATE(transaction_date), user_id')
            ->where($this->queryWhere())
            ->where('transaction_type', 'in', ['CARD_PAYMENT', 'CLEARING', 'settled', 'pending', 'AUTHORIZATION', 'declined'])
            ->where('status', 'in', ['SUCCESS', 'CANCELLED', 'settled', 'posted', 'pending', 'failed'])
            ->withSearch($this->setSearch(), $this->params)
            ->count();
    }

}